package com.yidu.BusinessParament.service.impl;


import com.yidu.BusinessParament.entity.Broker2;
import com.yidu.BusinessParament.mapper.BrokerMapper;
import com.yidu.BusinessParament.service.BrokerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @券商 Impl类
 * @version 1.0
 * @auther 林肯
 * @date 2022/12/8 -18:12
 */
@Service
public class BrokerServiceImpl implements BrokerService {
    @Resource
    private BrokerMapper brokerMapper;

    /**
     * 查询+分页
     * @param page 页码
     * @param limit 每页显示的条数
     * @param broker2 券商设置
     * @return
     */
    @Override
    public List<Broker2> selectBroker(int page, int limit, Broker2 broker2) {
        //创建Map对象
        Map map = new HashMap();
        //把值放入在Map集合里
        map.put("page",(page-1)*limit);
        map.put("limit",limit);
        //把按条件查询方法放入Map集合中
        map.put("sqlWhere",buildStrWhere(broker2));
        //返回值
        return brokerMapper.selectBroker(map);
    }

    /**
     * 生成条件字符串
     * @param broker2 生成条件的参数
     * @return 条件字符串  and fundId like '%%'
     */
    public String buildStrWhere(Broker2 broker2){
        StringBuffer strWhere = new StringBuffer("");
        if(broker2.getBrokerName ()!=null && !broker2.getBrokerName ().equals(""))
        {
            strWhere.append(" and brokerName like '%"+broker2.getBrokerName ()+"%'");
        }
        if (broker2.getBrokerId()!=null && !broker2.getBrokerId().equals("")){
            strWhere.append(" and brokerId="+broker2.getBrokerId());
        }
        return  strWhere.toString();
    }



    /**
     * 查行数 +名字查（按名字模糊查）
     * @param broker2 券商设置
     * @return
     */
    @Override
    public int selectCount(Broker2 broker2) {
        Map map=new HashMap();
        map.put("sqlWhere",buildStrWhere(broker2));
        int count = brokerMapper.selectCount(map);
        return count;
    }

    /**
     * 添加数据
     * @param broker2 券商设置
     * @return
     */
    @Override
    public int insertBroker(Broker2 broker2) {
        return brokerMapper.insertBroker(broker2);
    }

    /**
     * 删除数据
     * @param brokerId 券商编号
     * @return
     */
    @Override
    public int deleteBroker(String brokerId) {
        //将id转为数据
        String[] split = brokerId.split(",");
        //创建存放id的集合
        ArrayList<String> equityDataIdList = new ArrayList<String>();
        for (String id : split) {
            //将转换的id放入集合中
            equityDataIdList.add(id);
        }
       return brokerMapper.deleteBroker(equityDataIdList);
    }

    /**
     * 修改数据
     * @param broker2 券商设置
     * @return
     */
    @Override
    public int updateBroker(Broker2 broker2) {
        return brokerMapper.updateBroker(broker2);
    }

    /**
     * 获取最大ID
     * @return
     */
    public String selectMaxId(){
        return brokerMapper.selectMaxId();
    }
}
